package com.liusaidh.config;

import com.liusaidh.interceptor.MyInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 用来代替SpringMVC.xml配置文件
 */
@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/toLogin").setViewName("login");
        registry.addViewController("/toRegister").setViewName("register");
        registry.addViewController("/userLogin").setViewName("login");
        registry.addViewController("/backstage").setViewName("backstage");
        registry.addViewController("/backstage-blogCheck").setViewName("backstage-blogCheck");
        registry.addViewController("/backstage-blogManage").setViewName("backstage-blogManage");
        registry.addViewController("/backstage-cateManage").setViewName("backstage-cateManage");
        registry.addViewController("/home").setViewName("home");
        registry.addViewController("/home-new").setViewName("home-new");
        registry.addViewController("/hot").setViewName("hot");
        registry.addViewController("/hot-list").setViewName("hot-list");
        registry.addViewController("/hot-search").setViewName("hot-search");
        registry.addViewController("/index").setViewName("index");
        registry.addViewController("/message").setViewName("message");
        registry.addViewController("/message-comment").setViewName("message-comment");
        registry.addViewController("/personalPage-infoChange").setViewName("personalPage-infoChange");
        registry.addViewController("/personalPage-myFans").setViewName("personalPage-myFans");
        registry.addViewController("/personalPage-myFavs").setViewName("personalPage-myFavs");
        registry.addViewController("/personalPage-myFocus").setViewName("personalPage-myFocus");
        registry.addViewController("/personalPage-pswdChange").setViewName("personalPage-pswdChange");
        registry.addViewController("/personalPage-infoChange").setViewName("personalPage-infoChange");
        registry.addViewController("/search").setViewName("search");
        registry.addViewController("/search-user").setViewName("search-user");
        registry.addViewController("/unlogin").setViewName("unlogin");
        registry.addViewController("/unlogin-list").setViewName("unlogin-list");
        registry.addViewController("/unlogin-search").setViewName("unlogin-search");
        registry.addViewController("/tip-off").setViewName("tip-off");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/").excludePathPatterns( "/blog/getHotBlogForUnLogin", "/blog/getTheLatestHotBlogForUnLogin")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**")
                .excludePathPatterns("/static/**")
                .excludePathPatterns("/userLogin","/toRegister","/toLogin","/user/register","/user/getCode","/user/login");
    }
}
